/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path:string) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str:string) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url:string) {
  const reg =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str:string) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str:string) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str:string) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email:string) {
  const reg =
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str:any) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg:any[]) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}


/*
 * cs 2021-5-17
 * 验证是否为图片
 */
export function isImg(val:string) {
  const reg =
    /(.png|.jpg|.jpeg|.bmp|.tif|.gif|.pcx|.tga|.exif|.fpx|.svg|.psd|.cdr|.pcd|.dxf|.ufo|.eps|.ai|.raw|.WMF|.webp|.avif)$/i
  if (!val) {
    return false
  }
  if (reg.test(val)) {
    return true
  } else {
    return false
  }
}

/*
 * cs 2021-5-17
 * 验证是否为视频
 */
export function isVideo(val:string) {
  const reg = /(.AVI|.MOV|.MPG|.MPEG|.MPE|.DAT|.VOB|.ASF|.3GP|.MP4|.WMV|.ASF|.RM|.RMVB|.FLV|.MKV)$/i
  if (!val) {
    return false
  }
  if (reg.test(val)) {
    return true
  } else {
    return false
  }
}

/*
 * cs 2021-5-17
 * 验证是否为文档
 */
export function isDoc(val:string) {
  const reg =
    /(.pptx|.ppt|.pot|.potx|.pps|.ppsx|.dps|.dpt|.pptm|.potm|.ppsm|.xls|.et|.xlsx|.doc|.dot|.wps|.wpt|.docx|.dotx|.docm|.dotm|.pdf|.lrc|.c|.cpp|.h|.asm|.s|.java|.asp|.bat|.bat|.prg|.cmd|.rtf|.txt|.log|.xml|.htm|.html)$/i
  if (!val) {
    return false
  }
  if (reg.test(val)) {
    return true
  } else {
    return false
  }
}

/*
 * cs 2021-5-17
 * 验证是否为音频
 */
export function isAudio(val:string) {
  const reg = /(.mp3|.wma|.avi|.rm|.rmvb|.flv|.mpg|.mov|.mkv|.m4a)$/i
  if (!val) {
    return false
  }
  if (reg.test(val)) {
    return true
  } else {
    return false
  }
}

/*
 * cs 2021-5-17
 * 验证是否为压缩包
 */
export function isZip(val:string) {
  const reg = /(.zip|.rar)$/i
  if (!val) {
    return false
  }
  if (reg.test(val)) {
    return true
  } else {
    return false
  }
}

/*
 * cs 2020-11-18
 * 验证手机号
 */
export function isPhone(val:string) {
  const reg = /^1\d{10}$/;
  return reg.test(val);
}

/**
 * @param {string} code
 * @returns {Boolean}
 */
export function isCode(code:string) {
  const reg = /^[0-9a-zA-Z_]{1,}$/
  return reg.test(code)
}
/*
 * cs 2020-7-18
 * 验证html去所有标签
 */
export function matchReg(val:string) {
  const reg = /<\/?.+?\/?>/g;
  return val.replace(reg, '');
}
/*
 * cs 2021-3-17
 * 验证数组 负号,小数点
 */
export function isCSNumber(str:string) {
  const reg = /(^[1-9\-]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[1-9\-]\.[0-9]([0-9])?$)/
  return reg.test(str)
}
/*
 * cs 2020-5-15
 * 验证是否是金额
 */
export function isMoney(str:string) {
  const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
  return reg.test(str)
}
